package vn.com.demand.widget;

import java.sql.SQLException;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class SelectMultiItemsTableActivator extends TitleAreaDialog {

	private Text text;
	private ItemBrowserCommand browserCommand;
	private String dialogTitle;
	private String areaTitle;
	private String areaMessage;
	private SelectMultiItemTable selectMultiItemTable;
	private String[] selectedItemCode;
	/**
	 * Create the dialog
	 * @param parentShell
	 */
	public SelectMultiItemsTableActivator(Shell parentShell) {
		super(parentShell);
	}
	
	public SelectMultiItemsTableActivator(Shell parentShell, String sql) {
		this(parentShell, new ItemBrowserCommand(sql, null, null), "Browse and Select ", "Browser", "Browse and select rows then add to list and click OK");
	}
	
	public SelectMultiItemsTableActivator(Shell parentShell, int style) {
		super(parentShell);
	}
	
	public SelectMultiItemsTableActivator(Shell parentShell, ItemBrowserCommand command, String labelColumn) {
		this(parentShell, command, "Browse and Select ", "Browser", "Browse and select rows then add to list and click OK");
	}
	
	public SelectMultiItemsTableActivator(Shell parentShell, ItemBrowserCommand command, String dialogTitle, String areaTitle, String areaMessage) {
		super(parentShell);
		this.browserCommand = command;
		this.dialogTitle = dialogTitle;
		this.areaTitle = areaTitle;
		this.areaMessage = areaMessage;
	}
	
	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		setAreaTitle(dialogTitle);
	}
	
	/**
	 * Create contents of the dialog
	 * @param parent
	 */
	
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite area = (Composite) super.createDialogArea(parent);
		Composite container = new Composite(area, SWT.NONE);
		container.setLayout(new FormLayout());
		container.setLayoutData(new GridData(GridData.FILL_BOTH));

		final Label inputLabel = new Label(container, SWT.NONE);
		final FormData fd_inputLabel = new FormData();
		fd_inputLabel.right = new FormAttachment(0, 45);
		fd_inputLabel.bottom = new FormAttachment(0, 23);
		fd_inputLabel.top = new FormAttachment(0, 10);
		fd_inputLabel.left = new FormAttachment(0, 10);
		inputLabel.setLayoutData(fd_inputLabel);
		inputLabel.setText("Input");

		text = new Text(container, SWT.BORDER);
		final FormData fd_text = new FormData();
		fd_text.right = new FormAttachment(0, 200);
		fd_text.left = new FormAttachment(0, 50);
		fd_text.bottom = new FormAttachment(0, 26);
		fd_text.top = new FormAttachment(0, 7);
		text.setLayoutData(fd_text);

		try {
			selectMultiItemTable = new SelectMultiItemTable(container, SWT.NONE, browserCommand);
			final FormData fd_selectMultiItemTable = new FormData();
			fd_selectMultiItemTable.bottom = new FormAttachment(100, -5);
			fd_selectMultiItemTable.top = new FormAttachment(0, 45);
			fd_selectMultiItemTable.left = new FormAttachment(0, 5);
			fd_selectMultiItemTable.right = new FormAttachment(100, -5);
			selectMultiItemTable.setLayoutData(fd_selectMultiItemTable);
			selectMultiItemTable.setLayout(new FormLayout());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		final Button clearButton = new Button(container, SWT.NONE);
		clearButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				text.setText("");
			}
		});
		final FormData fd_clearButton = new FormData();
		fd_clearButton.right = new FormAttachment(0, 249);
		fd_clearButton.bottom = new FormAttachment(0, 30);
		fd_clearButton.top = new FormAttachment(0, 7);
		fd_clearButton.left = new FormAttachment(0, 205);
		clearButton.setLayoutData(fd_clearButton);
		clearButton.setText("&Clear");
		setTitle(dialogTitle);
		setMessage(areaMessage);
		makeAction();
		//
		return area;
	}

	private void makeAction() {
		text.addModifyListener(new ModifyListener() {

			public void modifyText(ModifyEvent event) {
				selectMultiItemTable.setFilterValue(text.getText());
			}
			
		});
	}
	
	/**
	 * Create contents of the button bar
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
				true);
		createButton(parent, IDialogConstants.CANCEL_ID,
				IDialogConstants.CANCEL_LABEL, false);
	}
	
	public String[] getSelectedItemCode() {
		return selectedItemCode;
	}

	public void setSelectedItemCode(String[] selectedItemCode) {
		this.selectedItemCode = selectedItemCode;
	}

	@Override
	protected void buttonPressed(int buttonId) {
		switch (buttonId) {
		case IDialogConstants.OK_ID:
			try {
				selectedItemCode = selectMultiItemTable.getSelectedItemsCode();
				super.buttonPressed(buttonId);
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;
		case IDialogConstants.CANCEL_ID:
			super.buttonPressed(buttonId);
		default :
			break;
		}
	}
	
	/**
	 * Return the initial size of the dialog
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(500, 494);
	}

	public ItemBrowserCommand getBrowserCommand() {
		return browserCommand;
	}

	public void setBrowserCommand(ItemBrowserCommand browserCommand) {
		this.browserCommand = browserCommand;
	}

	public String getDialogTitle() {
		return dialogTitle;
	}

	public void setDialogTitle(String dialogTitle) {
		this.dialogTitle = dialogTitle;
	}

	public String getAreaTitle() {
		return areaTitle;
	}

	public void setAreaTitle(String areaTitle) {
		this.areaTitle = areaTitle;
	}

	public String getAreaMessage() {
		return areaMessage;
	}

	public void setAreaMessage(String areaMessage) {
		this.areaMessage = areaMessage;
	}

}
